package com.baayso.bms2.permission;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.apache.commons.lang3.builder.ToStringBuilder;

import com.baayso.bms2.core.entity.IdEntity;
import com.baayso.bms2.role.Role;

/**
 * 权限表。
 * 
 * @author ChenFangjie(2015年9月20日 上午11:15:46)
 * 
 * @since 1.0.0
 * 
 * @version 1.0.0
 * 
 */
@Entity
@Table(name = "t_permission")
public class Permission extends IdEntity {

    private static final long serialVersionUID = -7755617203782831028L;

    @Column(length = 60, nullable = false)
    private String name;
    private String url; // 一个权限对应一个功能，一个功能对应一个URL（对功能的控制就是对URL的访问控制）

    @ManyToMany(targetEntity = Role.class)
    @JoinTable(name = "t_role_permission", joinColumns = { @JoinColumn(name = "permission_id", referencedColumnName = "id") }, inverseJoinColumns = {
            @JoinColumn(name = "role_id", referencedColumnName = "id") })
    private List<Role> roles;

    @ManyToOne
    private Permission parent; // 所属权限

    @OneToMany(targetEntity = Permission.class, mappedBy = "parent")
    private List<Permission> children; // 子权限

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public List<Role> getRoles() {
        return roles;
    }

    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }

    public Permission getParent() {
        return parent;
    }

    public void setParent(Permission parent) {
        this.parent = parent;
    }

    public List<Permission> getChildren() {
        return children;
    }

    public void setChildren(List<Permission> children) {
        this.children = children;
    }

}
